<!--
 * @Description: 任务中心
 * @Author: fxg
 * @Date: 2020-06-29 11:18:28
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-03-23 10:41:02
-->
<template>
  <div class="pages_content table taskyTypes">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <transition name="from"></transition>
            <span v-show="!showCardNav"><i style="margin-right: 5px;" class="el-icon-arrow-down"></i></span>
            <transition name="to"></transition>
            <span v-show="showCardNav"><i style="margin-right: 5px;" class="el-icon-arrow-up"></i></span>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <span style="display: inline-block;margin-bottom: 10px;">
                <span style="margin-right: 12px;">名称：</span>
                <el-input v-model="tableParam.taskName" style="width: 200px;" clearable placeholder="请输入" />
              </span>
              <span style="margin-left: 36px;">
                <span style="margin-right: 12px;">提交人</span>
                <el-input v-model="tableParam.createUserName" style="width: 200px;" clearable placeholder="请输入" />
              </span>
              <span style="margin-left: 36px;">
                <span style="margin-right: 12px;">编码</span>
                <el-input v-model="tableParam.code" style="width: 200px;" clearable placeholder="请输入" />
              </span>

              <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), queryPage()">查 询</el-button>
              <el-button size="medium" class="bottom_all_two" @click="reset()">重 置</el-button>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button v-show="type!==6" size="small" class="bottom_one" @click="collection()">收藏任务</el-button>
              <el-button v-show="type===6" size="small" class="bottom_one" @click="collection()">取消收藏</el-button>
              <el-button size="small" style="margin-left: 10px;" class="bottom_all_two" @click="advancedSearch">高级搜索</el-button>
            </div>
            <div>
              <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                <el-link :underline="false">
                  <span class="el-icon--left"><i class="iconfont iconicon_button_filtrate"></i></span>
                  筛选
                </el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in table" :key="index">
                    <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-link :underline="false" @click="Download()">
                <span class="el-icon--left"><i class="iconfont iconicon_button_export1"></i></span>
                导出
              </el-link>
            </div>
          </div>
          <div class="card_info" :style="style">
            <el-table v-loading="loading" :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
              <el-table-column align="center" type="selection" width="50"></el-table-column>
              <template v-for="(item, index) in table">
                <el-table-column v-if="item.status" :key="index" :fixed="item.label === '操作'?'right':false" :label="item.label" align="center" :width="item.width">
                  <template slot-scope="scope">
                    <el-button v-if="item.label === '操作'" class="bottom_one" size="mini" @click="hanldeClick(scope.row)">处理</el-button>
                    <span v-if="item.prop==='taskStatus'">
                      <el-tag v-if='scope.row.taskStatus===1' type="success">保存中</el-tag>
                      <el-tag v-if='scope.row.taskStatus===2' type="info">申请中</el-tag>
                      <el-tag v-if='scope.row.taskStatus===3' type="warning">已结束</el-tag>
                      <el-tag v-if='scope.row.taskStatus===4' type="danger">已撤销</el-tag>
                    </span>
                    <span v-if="item.prop==='timekeeping'">{{scope.row[item.prop]*1===1?'计时':'非计时'}}</span>
                    <span v-if="item.label !== '操作'&&item.prop !== 'taskStatus'&&item.prop !== 'timekeeping'">{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
          <div class="card_bottom">
            <div class="card_bottom_left">
              <div class="top_page_totle">总共{{ total }}条</div>
              <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="queryPage()"></el-pagination>
            </div>
            <div>
              <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="queryPage()">刷新</el-button>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" width="50%" :close-on-click-modal="false" :before-close="resefrom">
      <span slot="title" class="dialog-title">高级搜索</span>
      <div class="dialoginfo">
        <el-form ref="dialogForm" :model="seniorfrom" label-width="90px">
          <!-- <el-form-item prop="processManagerId">
            <span slot="label">任务分类</span>
            <el-select v-model="seniorfrom.processManagerId" placeholder="请选择">
              <el-option v-for="item in processManagerarr" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item prop="time">
            <span slot="label">日期范围</span>
            <el-date-picker v-model="seniorfrom.time" unlink-panels value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
          <el-form-item prop="code">
            <span slot="label">单据编号</span>
            <el-input v-model="seniorfrom.code" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="taskName">
            <span slot="label">任务主题</span>
            <el-input v-model="seniorfrom.taskName" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="createUserName">
            <span slot="label">发起人</span>
            <el-input v-model="seniorfrom.createUserName" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="userOrganizationId">
            <span slot="label">组织</span>
            <div class="dialoginfo-info-info scroll">
              <el-tree :data="treearr" :props="defaultProps" class="tree-line"  :indent="0"  :expand-on-click-node="false" @node-click="handleNodeClick1">
                <span slot-scope="{ node, data }" class="custom-tree-node">
                  <i :class="data.icon" :style="'color:' + data.color"></i>
                  <span>{{ node.label }}</span>
                </span>
              </el-tree>
            </div>
          </el-form-item>
          <el-form-item prop="dataSource">
            <span slot="label">流程</span>
            <div class="dialoginfo-info-info scroll">
              <el-tree :data="treeArry" :props="defaultProps" class="tree-line"  :indent="0"  :expand-on-click-node="false" @node-click="handleNodeClick2">
                <span slot-scope="{ node, data }" class="custom-tree-node">
                  <i :class="data.icon" :style="'color:' + data.color"></i>
                  <span>{{ node.label }}</span>
                </span>
              </el-tree>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resefrom()">取 消</el-button>
        <el-button class="bottom_all" @click="save(),dialogVisible=false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import breaDcrumb from '@/components/breadcrumb';
import { mapGetters } from 'vuex';
import { formatTreeData } from '@/utils/utils';
import { getFlowsList } from '@/api/Signed/flows';
import { queryPage as queryorganizationPage } from '@/api/basedata/organization.js';
import { queryPage } from '@/api/basedata/business';
import styles from '@/app.scss';
import {
  backlogQueryPage,
  dueOutQueryPage,
  myOutTaskQueryPage,
  submitTaskQueryPage,
  finishedTaskQueryPage,
  collectQueryPage,
  tailAfterTaskQueryPage,
  taskCollectByUser,
} from '@/api/task/taskType.js';
export default {
  components: {
    breaDcrumb,
  },
  props: ['type'], // 1.待办、2.待发、3.本人发起、4.提交任务、5已办、6.收藏 7.跟踪事项
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }
        ]
      },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      treearr: [],
      treeArry: [],
      iconlist: ['icon iconfont icontree_icon_structure', 'icon iconfont icontree_icon_department'],
      iconcolr: [styles.botton_color_one, styles.botton_color_two, styles.botton_color_three, styles.botton_color_four, styles.botton_color_five],
      dialogVisible: false,
      /*判断收起展开*/
      showCardNav: true,
      /*搜索关键字段*/
      /*列表展示*/
      table: [
        { label: '名称', width: '400', prop: 'taskName', status: true },
        { label: '编码', width: '200', prop: 'receiptNumber', status: true },
        { label: '任务分类', width: '120', prop: 'taskTypeName', status: true },
        { label: '提交人', width: 'auto', prop: 'createUserName', status: true },
        { label: '提交人机构', width: '150', prop: 'userOrganizationName', status: true },
        { label: '提交人部门', width: '150', prop: 'userDepartmentName', status: true },
        { label: '状态', width: 'auto', prop: 'taskStatus', status: true },
        { label: '提交时间', width: 'auto', prop: 'createTime', status: true },
        { label: '计时', width: 'auto', prop: 'timekeeping', status: true },
        { label: '执行人', width: 'auto', prop: 'updateUserName', status: true },
        { label: '执行时间', width: '200', prop: 'updateTime', status: true },
        { label: '操作', width: '100', prop: '', status: true }
      ],
      /*列表选中的数据集*/
      tableParam: {
        page: 1,
        pageSize: 12,
        code: '',
        createUserName: '',
        taskName: ''
      },
      seniorfrom: {
        taskTypeId: '',
        createTime: '',
        updateTime: '',
        time: [],
      },
      processManagerarr: [],
      time: [],
      exits: [],
      total: 0,
      tableData: [],
      loading: false,
      systemList: [],
      userOrganizationId: '',
      dataSource: '',
    };
  },
  mounted() {
    this.queryPage();
    const params = {
      page: 1,
      pageSize: 10000
    };
    queryorganizationPage(params).then(res => {
      if (res.code === 1001) {
        const { data } = res.data;
        this.treearr = formatTreeData(data, 'id', 'parentId', 'children', 'parent_id');
        this.changgeicon(this.treearr, 0);
      }
    });

  },
  methods: {
    advancedSearch() {
      this.seniorfrom = {
        createTime: '',
        updateTime: '',
        time: [],
      }
      this.userOrganizationId = '';
      this.dataSource = '';
      queryPage({ page: 1, pageSize: 1000 }).then(res => {
        if (res.code === 1001) {
          this.systemList = res.data.data.map((item) => (
            { ...item, parentId: 0 }
          ));
        }
      }).then(() => {
        getFlowsList({ page: 1, pageSize: 1000 }).then(res => {
          if (res.code === 1001) {
            const newData = res.data.data.map((item) => (
              item.parentId === 'parent_id' ? { ...item, parentId: item.systemId } : { ...item }
            ))
            this.treeArry = formatTreeData(newData.concat(this.systemList), 'id', 'parentId', 'children', 0);
            this.dialogVisible = true;
          }
        });
      });
    },
    handleNodeClick1(_, data) {
      this.userOrganizationId = data.data.id;
    },
    handleNodeClick2(_, data) {
      this.dataSource = data.data.code;
    },
    changgeicon(data, num) {
      data.forEach(item => {
        if (item.children) {
          item.icon = this.iconlist[num];
          item.color = this.iconcolr[num % 5];
          this.changgeicon(item.children, num + 1);
        } else {
          item.color = this.iconcolr[num % 5];
          item.icon = 'icon iconfont icontree_icon_team';
        }
      });
    },
    resefrom() {
      this.dialogVisible = false;
      this.$refs.dialogForm.resetFields();
    },
    save() {
      const params = {
        ...this.seniorfrom,
        userOrganizationId: this.userOrganizationId,
        dataSource: this.dataSource,
        createTime: this.seniorfrom.time[0],
        updateTime: this.seniorfrom.time[1],
      }
      this.loading = true;
      switch (this.type) {
        case 1: { // 待办
          backlogQueryPage(params).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 2: { // 待发
          dueOutQueryPage(params).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 3: {
          myOutTaskQueryPage(params).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 4: {
          submitTaskQueryPage(params).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 5: {
          finishedTaskQueryPage(params).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 6: {
          collectQueryPage(params).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 7: {
          tailAfterTaskQueryPage(params).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
      }
    },
    Download() {
      switch (this.type) {
        case 1: {
          this.$Download('/workflow/system/sign/task/center/exportBacklogTask', this.tableParam);
          break;
        }
        case 2: {
          this.$Download('/workflow/system/sign/task/center/exportDueOutTask', this.tableParam);
          break;
        }
        case 3: {
          this.$Download('/workflow/system/sign/task/center/exportMyOutTask', this.tableParam);
          break;
        }
        case 4: {
          this.$Download('/workflow/system/sign/task/center/exportSubmitTask', this.tableParam);
          break;
        }
        case 5: {
          this.$Download('/workflow/system/sign/task/center/exportFinishedTask', this.tableParam);
          break;
        }
        case 6: {
          this.$Download('/workflow/system/sign/task/center/exportCollectTask', this.tableParam);
          break;
        }
        case 7: {
          this.$Download('/workflow/system/sign/task/center/exportTailAfterTask', this.tableParam);
          break;
        }
      }
      //window.location.href = process.env.VUE_APP_BASE_API + 'file/download/' + url;
    },
    collection() {
      if (!this.exits.length) {
        this.$message.warning('请先选择数据');
        return
      }
      taskCollectByUser(this.exits).then((res) => {
        if (res.code == 1001) {
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.queryPage();
        }
      });
    },
    /**
     * @Description: 处理流程
     * @Param: 
     * @Author: wangwangwang
     */
    hanldeClick(data) {
      if (data.formType === 1) { // 自定义表单
        const query = {
          processManagerId: data.processManagerId,
          processTaskId: data.id,
          processVersionId: data.versionNumber,
          receiptNumber: data.receiptNumber,
          taskName: data.taskName,
          taskStatus: data.taskStatus,
          createTime: data.createTime,
          createUserName: data.createUserName,
          type: 2,
          taskType: this.type,
          isReadOnly: (this.type === 3 || this.type === 5 || this.type === 4 || this.type === 6 || this.type === 7) ? 1 : 0
        }
        /**
         * @Description: 如果是待发流程往发起跳转
         * @param {*}
         * @Author: wangwangwang
         */
        if (this.type === 2) {
          this.$router.push({ name: 'taskCenter', query });
          return
        }
        this.$router.push({ name: 'taskDetails', query });
        return
      }
      const query = {
        processManagerId: data.processManagerId,
        processTaskId: data.id,
        processVersionId: data.versionNumber,
        receiptNumber: data.receiptNumber,
        dataContent: data.dataContent,
        createTime: data.createTime,
        createUserName: data.createUserName,
        taskName: data.taskName,
        taskStatus: data.taskStatus,
        type: 2, // 1为发起 2为详情审批
        taskType: this.type,
        isReadOnly: (this.type === 3 || this.type === 5 || this.type === 4 || this.type === 6 || this.type === 7) ? 1 : 0
      }
      this.$router.push({ name: data.dataSource, query });
    },
    /*重置*/
    reset() {
      this.tableParam.code = '';
      this.tableParam.createUserName = '';
      this.tableParam.taskName = '';
      this.queryPage();
    },
    /*获取列表*/
    queryPage() {
      this.loading = true;
      switch (this.type) {
        case 1: { // 待办
          backlogQueryPage(this.tableParam).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 2: { // 待发
          dueOutQueryPage(this.tableParam).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 3: {
          myOutTaskQueryPage(this.tableParam).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 4: {
          submitTaskQueryPage(this.tableParam).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 5: {
          finishedTaskQueryPage(this.tableParam).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 6: {
          collectQueryPage(this.tableParam).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
        case 7: {
          tailAfterTaskQueryPage(this.tableParam).then(res => {
            if (res.code === 1001) {
              this.total = res.data.totalRows;
              this.tableData = res.data.data;
            }
            this.loading = false;
          });
          break;
        }
      }
    },
    /*批量选择*/
    handleSelectionChange(val) {
      this.collects = val.map(item => {
        return item.receiptNumber;
      });
      this.exits = val.map(item => {
        let obj = new Object();
        (obj.createUserId = item.createUserId), (obj.receiptNumber = item.receiptNumber);
        return obj;
      });
    }
  },
  computed: {
    ...mapGetters({
      screenWidth: 'getscreenWidth',
      screenHeight: 'getscreenHeight',
      Collapse: 'getsCollapse'
    }),
    style() {
      let num = 0;
      if (this.Collapse) {
        num = this.screenWidth - 124;
      } else {
        num = this.screenWidth - 220;
      }
      return 'width:' + num + 'px;';
    }
  }
};
</script>
<style lang="scss" scoped>
.iconfont {
  font-size: 14px;
  padding-right: 5px;
}
.taskyTypes {
  .dialoginfo {
    .el-input,
    .el-select {
      width: 200px;
    }
  }
}
.dialoginfo-info-info {
  max-height: 500px;
  overflow: auto;
}
</style>
